/**
 * 用户模块接口文件
 */

import axios from './request';
import { IPage, IPageResult } from './common';

/**
 * 登录用户信息
 */
export interface ILoginUser {
    username: string;
    password: string;
}

/**
 * 用户基本信息接口
 */
export interface IUserBase {
    role_id: number;
    status: string;
    username: string;
    email: string;
    phone: string;
}

/**
 * 添加用户类型
 */
export type AddUserType = Exclude<ILoginUser & IUserBase, 'status'>;

/**
 * 修改用户类型
 */
export type UpdateUserType = Pick<IUser, 'id' | 'role_id' | 'email' | 'phone'>;

/**
 * 用户信息接口
 */
export interface IUser extends ILoginUser, IUserBase {
    id: number;
    role_name: string;
    creation_time: Date;
}

/**
 * 用户搜索接口
 */
export interface IUserSearch extends IUserBase, IPage { }

/**
 * 修改密码接口
 */
export interface IUserPassword {
    id: number;
    password: string;
    confirmPassword: string;
}

/**
 * 登录
 * @param {*} user 用户登录信息
 * @returns 登录
 */
export const login = (user: ILoginUser): Promise<IUser> => axios.post('/users/login', user);

/**
 * 用户搜索
 * @param queryModel 
 * @returns 
 */
export const search = (queryModel: IUserSearch): Promise<IPageResult<IUser>> => axios.get('/users/search', { params: queryModel });

/**
 * 添加用户
 * @param user 
 * @returns 
 */
export const add = (user: AddUserType) => axios.post('/users/add', user);

/**
 * 修改用户
 * @param user 
 * @returns 
 */
export const update = (user: UpdateUserType) => axios.put('/users/update', user);

/**
 * 修改密码
 * @param user 
 * @returns 
 */
export const updatePassword = (user: IUserPassword) => axios.put('/users/updatePassword', user);

/**
 * 更改状态
 * @param id 
 * @param status 
 * @returns 
 */
export const changeStatus = (id: number, status: string) => axios.put('/users/changeStatus', { id, status });

/**
 * 删除用户
 * @param id 
 * @returns 
 */
export const deleteUser = (id: number) => axios.delete('/users/delete/' + id);